<template>
  <div class="tags-add">
    <el-dialog
      :title="`${this.formList.id ? '修改' : '新增'} 目录`"
      :visible="dialogVisible"
      width="30%"
      @close="closemet"
      @open="openaddd"
    >
      <template>
        <el-form label-width="80px" ref="from" :model="formList" :rules="rules">
          <el-form-item label="所属学科" prop="subjectID">
            <el-select placeholder="请选择" v-model="formList.subjectID" style="width:100%">
              <el-option
                v-for="item in hireList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称" prop="directoryName" style="width: 100%">
            <el-input v-model="formList.directoryName" />
          </el-form-item>
        </el-form>
      </template>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closemet">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { catalogueAddAPI, catalogueUpdateAPI, catalogueDetailAPI } from '@/api/hmmm/directorys.js'
import { subjectSimpleAPI } from '@/api/hmmm/subjects.js'
export default {
  name: 'TagsAdd',
  props: {
    dialogVisible: {
      type: Boolean
    }
  },
  data() {
    return {
      formList: {
        subjectID: '',
        directoryName: ''
      },
      rules: {
        directoryName: [{ required: true, message: '请输入目录名称', trigger: 'blur' }],
        subjectID: [{ required: true, message: '请选择学科', trigger: 'blur' }]
      },
      hireList: []
    }
  },
  methods: {
    closemet() {
      this.$emit('add')
      // 清空验证
      this.$refs.from.resetFields()
      // 清空内容
      this.formList = {
        subjectID: '',
        directoryName: ''
      }
    },
    confirm() {
      this.$refs.from.validate(async valid => {
        if (valid) {
          if (!this.formList.id) {
            await catalogueAddAPI(this.formList)
          } else {
            await catalogueUpdateAPI(this.formList)
          }
          this.$message.success(`${this.formList.id ? '编辑' : '新增'} 成功`)
          this.$refs.from.resetFields()
          this.closemet()
          this.appForm()
          this.$emit('update-list')
        }
      })
    },
    appForm() {
      this.formList = {
        subjectID: '',
        directoryName: ''
      }
    },
    // 渲染弹窗下拉列表
    async openaddd() {
      const res = await subjectSimpleAPI()
      this.hireList = res.data
    },
    async fetList(data) {
      const res = await catalogueDetailAPI(data)
      this.formList = res.data
      this.$emit('update-list')
    }
  }
}
</script>

<style scoped></style>
